import { Body, Controller, Post, Request, UseGuards, UsePipes } from '@nestjs/common';
import { UserService } from './user.service';
import { AuthService } from '../auth/auth.service';
import { AuthGuard } from '@nestjs/passport';
import { ValidationPipe } from '../../pipe/validation.pipe';
import { RegisterInfoDTO } from './user.dto';

@Controller('user')
export class UserController {
    constructor(
        private readonly authService: AuthService,
        private readonly userService: UserService
    ) {}
    
    // JWT验证 - Step 1: 用户请求登录
    @Post('login')
    async login(@Body() loginParmas: any) {
        console.log(`JWT验证 - Step 1: 用户请求登录`);
        const authResult = await this.authService.validateUser(
            loginParmas.username,
            loginParmas.password
        )
        switch (authResult.code) {
            case 1:
                return this.authService.certificate(authResult.user)
            case 2:
                return {
                    code: 600,
                    message: '账号或密码不正确'
                }
            default:
                return {
                    code: 600,
                    msg: `查无此人`,
                };
        }
    }

    // @UseGuards(AuthGuard('jwt'))
    @UsePipes(new ValidationPipe())
    @Post('register')
    async register(@Body() body: RegisterInfoDTO) {
        return this.userService.register(body)
    }

    @UseGuards(AuthGuard('jwt'))
    @Post('getMyInfo')
    async getMyInfo(@Body() body: any, @Request() req: any) {
        return this.userService.getMyInfo(req)
    }
}
